home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4411 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: Sun, 04 Feb 96 12:23:07 GMT
  6. Organization: none
  7. Message-ID: <823436587snz@genesis.demon.co.uk>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <TANMOY.96Jan28100725@qcd.lanl.gov> <4eo8m7$647@ns.RezoNet.NET>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4eo8m7$647@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
  15.  
  16. >Unfortunately, many uses of "void *" are far bigger loopholes in type 
  17. >safety than a cast.
  18.  
  19. Converting to and from void * is in general no more or less safe than casting
  20. to and from char *. It may be that some compilers warn about a cast from
  21. char * to a pointer with stricter alignment but the language makes no
  22. requirement about this. In the particular case of malloc's return value
  23. you are guaranteed suitable alignment for any object, so it isn't an issue.
  24.  
  25. > Compilers or lint will pick up most erroneous uses 
  26. >of a cast.  A wrong pointer type as the value of a void * function 
  27. >parameter can only be debugged at runtime
  28.  
  29. How would avoiding void * and using casts help in this particular case?
  30. If you need to pass pointers to different types through an argument you
  31. must use a paraneter type that they can all be converted to. Typically that
  32. can be either void * or char * (requiring a cast). Casting to char *
  33. doesn't create any alignment issues so it is unlikely that the compiler
  34. will generate any more useful diagnostics in this case than with using void *.
  35. In fact you might lose some essential diagnostics (e.g. conversions from
  36. function pointers).
  37.  
  38. Anyway the reasons why I don't cast the return value of malloc are:
  39.  
  40. 1. it results in less cluttered and on balance more readable code. In the
  41.    local area of a malloc call I typically don't care what the actual
  42.    type is, only that the correct amount of memory is allocated for the
  43.    object concerned.
  44.  
  45. 2. it increases the number of things I can get wrong. An explicit type cast
  46.    is redundant information. All the compiler can do is test the cast
  47.    type against the type of the pointer being assigned to. There is no reason
  48.    why the cast type should be any more accurate than the pointer type.
  49.    If the pointer type was wrong that would very likely show up in the rest
  50.    of the code that tries to use the pointer.
  51.  
  52. 3. Allowed void * conversions map pretty well onto the target pointer types
  53.    that it is reasonable to use with malloc. Casts are less discerning, even
  54.    if a particular compiler is helpful enough to warn about most problem
  55.    cases. You're putting yourself in the hands of the compiler writer
  56.    especially if you ever have to change versions or development
  57.    platforms.
  58.  
  59. I'd certainly say that when advocating no casts you should also advocate
  60. using the form:
  61.  
  62.    ptr = malloc(numelems * sizeof *ptr);
  63.  
  64. which deals with most issues.
  65.  
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.